package com.xie.smfs.mixin;

import com.xie.smfs.common.events.PlayerEvents;
import com.xie.smfs.api.IPlayerData;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtInt;
import net.minecraft.nbt.NbtFloat;
import net.minecraft.nbt.NbtElement;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.xie.smfs.effect.SpiritAttributes;
import net.minecraft.entity.attribute.EntityAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Mixin(PlayerEntity.class)


public class PlayerEntityMixin implements IPlayerData {
    @Unique
    private static final Logger LOGGER = LoggerFactory.getLogger(PlayerEntityMixin.class);
    @Unique
    private int customProperty = 0;

    // 基础属性实现
    @Override
    public int getCustomProperty() {
        return customProperty;
    }

    @Override
    public void setCustomProperty(int value) {
        this.customProperty = value;
    }

    // 灵异属性实现 - 通过PlayerEvents访问属性系统
    @Override
    public float getSpiritResistance() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getSpiritAttribute(player, SpiritAttributes.SPIRIT_RESISTANCE);
    }

    @Override
    public void setSpiritResistance(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setSpiritAttribute(player, SpiritAttributes.SPIRIT_RESISTANCE, value);
    }

    @Override
    public float getSpiritDamage() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getSpiritAttribute(player, SpiritAttributes.SPIRIT_DAMAGE);
    }

    @Override
    public void setSpiritDamage(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setSpiritAttribute(player, SpiritAttributes.SPIRIT_DAMAGE, value);
    }

    @Override
    public float getCurrentSpirit() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getCurrentSpirit(player);
    }

    @Override
    public void setCurrentSpirit(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setCurrentSpirit(player, value);
    }

    @Override
    public float getMaxSpirit() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getMaxSpirit(player);
    }

    @Override
    public void setMaxSpirit(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setMaxSpirit(player, value);
    }

    @Override
    public float getRevivalFactor() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getSpiritAttribute(player, SpiritAttributes.REVIVAL_FACTOR);
    }

    @Override
    public void setRevivalFactor(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setSpiritAttribute(player, SpiritAttributes.REVIVAL_FACTOR, value);
    }

    @Override
    public float getSanity() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getSpiritAttribute(player, SpiritAttributes.SANITY);
    }

    @Override
    public void setSanity(float value) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.setSpiritAttribute(player, SpiritAttributes.SANITY, value);
    }

    // NBT数据操作实现
    @Override
    public NbtCompound getSpiritData() {
        PlayerEntity player = (PlayerEntity) (Object) this;
        return PlayerEvents.getSpiritAttributes(player);
    }

    @Override
    public void setSpiritData(NbtCompound data) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        PlayerEvents.saveDataToPlayer(player, data);
    }

    @Override
    public void copyFrom(IPlayerData other) {
        this.setCustomProperty(other.getCustomProperty());
        this.setSpiritResistance(other.getSpiritResistance());
        this.setSpiritDamage(other.getSpiritDamage());
        this.setCurrentSpirit(other.getCurrentSpirit());
        this.setMaxSpirit(other.getMaxSpirit());
        this.setRevivalFactor(other.getRevivalFactor());
        this.setSanity(other.getSanity());
        this.setSpiritData(other.getSpiritData().copy());
    }

    @Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
    private void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        NbtCompound spiritData = PlayerEvents.getSpiritAttributes(player);
        nbt.put(PlayerEvents.SMFS_DATA_KEY, spiritData);
        LOGGER.info("已将灵异数据（{}个键）保存到NBT", spiritData.getKeys().size());
        for (String key : spiritData.getKeys()) {
            NbtElement element = spiritData.get(key);
            if (element instanceof NbtInt) {
                LOGGER.info("保存时槽位/属性: {} = {}", key, ((NbtInt) element).intValue());
            } else if (element instanceof NbtFloat) {
                LOGGER.info("保存时槽位/属性: {} = {}", key, ((NbtFloat) element).floatValue());
            } else {
                LOGGER.info("保存时槽位/属性: {} = {}", key, element);
            }
        }
        nbt.putInt("customProperty", this.customProperty);
    }

    @Inject(method = "readCustomDataFromNbt", at = @At("TAIL"))
    private void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
        PlayerEntity player = (PlayerEntity) (Object) this;
        if (nbt.contains(PlayerEvents.SMFS_DATA_KEY)) {
            NbtCompound spiritData = nbt.getCompound(PlayerEvents.SMFS_DATA_KEY);
            PlayerEvents.setSpiritAttributes(player, spiritData);
            LOGGER.info("已从NBT加载灵异数据（{}个键）", spiritData.getKeys().size());
            for (String key : spiritData.getKeys()) {
                NbtElement element = spiritData.get(key);
                if (element instanceof NbtInt) {
                    LOGGER.info("加载时槽位/属性: {} = {}", key, ((NbtInt) element).intValue());
                } else if (element instanceof NbtFloat) {
                    LOGGER.info("加载时槽位/属性: {} = {}", key, ((NbtFloat) element).floatValue());
                } else {
                    LOGGER.info("加载时槽位/属性: {} = {}", key, element);
                }
            }
        this.customProperty = nbt.getInt("customProperty");
        } else {
            PlayerEvents.initSpiritData(player);
            LOGGER.info("已为玩家初始化新的灵异数据");
        }
    }
}